package com.zl.tby.service.impl;

import com.zl.tby.core.base.bean.OptionType;
import com.zl.tby.core.base.bean.TbyPageResult;
import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyVillageInfoQuery;
import com.zl.tby.model.TbyVillageInfo;
import com.zl.tby.repository.TbyVillageInfoRepo;
import com.zl.tby.service.ITbyVillageInfoService;
import com.zl.tby.vo.TbyVillageInfoForm;
import org.noear.solon.annotation.Component;
import org.noear.solon.data.annotation.Tran;
import org.noear.wood.DbContext;
import org.noear.wood.IPage;
import org.noear.wood.annotation.Db;
import org.noear.wood.utils.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 社区共管接口实现类
 * @author sunjiaxing
 * @version 1.0
 * @since 2024/8/22 00:23
 */
@Component
public class TbyVillageInfoServiceImpl implements ITbyVillageInfoService {

    @Db
    private TbyVillageInfoRepo tbyVillageInfoRepo;
    @Db
    private DbContext db;

    @Override
    @Tran
    public R<TbyVillageInfo> add(TbyVillageInfoForm form) {
        TbyVillageInfo entity = form.createEntity();
        entity.setCreateBy(ToolsUtil.getLoginUserId());
        entity.setCreateTime(LocalDateTime.now());
        long id = tbyVillageInfoRepo.insert(entity, true);
        entity.setId(id);
        return R.ok(entity);
    }

    @Override
    @Tran
    public R<TbyVillageInfo> update(TbyVillageInfoForm form) {
        TbyVillageInfo entity = this.get(form.getId());
        if (entity == null) {
            return R.failed("数据不存在");
        }
        form.updateEntity(entity);
        entity.setUpdateBy(ToolsUtil.getLoginUserId());
        entity.setUpdateTime(LocalDateTime.now());
        tbyVillageInfoRepo.updateById(entity, false);
        return R.ok(entity);
    }

    @Override
    public R<Void> delete(Long id) {
        int flag = tbyVillageInfoRepo.deleteById(id);
        return flag > 0 ? R.ok() : R.failed("删除失败");
    }

    @Override
    public TbyVillageInfo get(Long id) {
        return tbyVillageInfoRepo.selectById(id);
    }

    @Override
    public TbyPageResult<TbyVillageInfo> page(TbyVillageInfoQuery query) {
        IPage<TbyVillageInfo> iPage = tbyVillageInfoRepo.selectPage(query.getOffset(), query.getSize(), mp -> {
            mp.whereTrue()
                    .andIf(StringUtils.isNotEmpty(query.getVillageName()), "village_name like ?", "%" + query.getVillageName() + "%")
                    .andIf(ToolsUtil.isNotNull(query.getVillageType()), "village_type = ?", query.getVillageType())
                    .andIf(ToolsUtil.isNotNull(query.getParentId()), " parent_id = ?", query.getParentId());
            if (StringUtils.isNotEmpty(query.getOrder())) {
                mp.orderBy(query.getOrder());
            }
        });
        return new TbyPageResult(iPage, query);
    }

    @Override
    public List<TbyVillageInfo> list(TbyVillageInfoQuery query) {
        return tbyVillageInfoRepo.selectList(mp -> {
            mp.whereTrue()
                    .andIf(StringUtils.isNotEmpty(query.getVillageName()), "village_name like ?", "%" + query.getVillageName() + "%")
                    .andIf(ToolsUtil.isNotNull(query.getVillageType()), "village_type = ?", query.getVillageType())
                    .andIf(ToolsUtil.isNotNull(query.getParentId()), " parent_id = ?", query.getParentId());
            if (StringUtils.isNotEmpty(query.getOrder())) {
                mp.orderBy(query.getOrder());
            }
        });
    }

    @Override
    public List<TbyVillageInfo> listNoContent(TbyVillageInfoQuery query) {
        try {
            return db.table("tby_village_info").whereTrue()
                    .andIf(StringUtils.isNotEmpty(query.getVillageName()), "village_name like ?", "%" + query.getVillageName() + "%")
                    .andIf(ToolsUtil.isNotNull(query.getVillageType()), "village_type = ?", query.getVillageType())
                    .andIf(ToolsUtil.isNotNull(query.getParentId()), " parent_id = ?", query.getParentId())
                    .selectList("id, village_name, village_type, longitude, latitude", TbyVillageInfo.class);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ArrayList<>();
    }

    @Override
    public List<OptionType> options(TbyVillageInfoQuery query) {
        return TbyVillageInfoForm.recurRouteOptions(0L, this.tbyVillageInfoRepo.selectList(mp -> {
            mp.whereTrue().andIf(ToolsUtil.isNotNull(query.getParentId()), " parent_id = ?", query.getParentId())
                            .andIf(ToolsUtil.isNotNull(query.getParentId()), " parent_id = ?", query.getParentId());
        }));
    }

    @Override
    public void updatePersonById(Long villageId, Long registeredPopulation, Long permanentPopulation, Long labourForce) {
        TbyVillageInfo village = new TbyVillageInfo();
        village.setId(villageId);
        village.setRegisteredPopulation(registeredPopulation);
        village.setPermanentPopulation(permanentPopulation);
        village.setLabourForce(labourForce);
        this.tbyVillageInfoRepo.updateById(village, true);
    }
}
